//
//  CustomTableViewCell.m
//  10_CustomTableViewCell
//
//  Created by 徐昊 on 16/7/27.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "CustomTableViewCell.h"
// 使用@class声明的类, 需要在.m中去import, 要不然无法使用
#import "NewsObject.h"

#define kWidth self.contentView.frame.size.width
#define kHeight self.contentView.frame.size.height
@interface CustomTableViewCell ()
@property (nonatomic, strong) UIView *customView;

@end

@implementation CustomTableViewCell

// 自定义TableViewCell的初始化方法
// initWithStyle:reuseIndentifier:
// 注意: 不要是同initWithFrame:
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // TableViewCell的默认大小 宽320 高44
        // 在初始化时, cell的frame是初始值, 不能为自定义视图布局
        NSLog(@"初始化时, cell的frame是%@", NSStringFromCGRect(self.frame));
        // 初始化方法中, 只负责创建
        // 添加子View
        _customView = [[UIView alloc] initWithFrame:CGRectZero];
        _customView.backgroundColor = [UIColor orangeColor];
        [self.contentView addSubview:_customView];
        _newsImageView = [[UIImageView alloc] init];
        _newsImageView.backgroundColor = [UIColor blackColor];
        [self.contentView addSubview:_newsImageView];
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.backgroundColor = [UIColor greenColor];
        [self.contentView addSubview:_titleLabel];
        _descriptionLabel = [[UILabel alloc] init];
        _descriptionLabel.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_descriptionLabel];
        _numberLabel = [[UILabel alloc] init];
        _numberLabel.backgroundColor = [UIColor cyanColor];
        [self.contentView addSubview:_numberLabel];
    }
    return self;
}
// 坐标有改变就要调用这个方法
- (void)layoutSubviews {
    // 千万不要忘记 super
    [super layoutSubviews];
    NSLog(@"%s时, cell的frame是%@", __FUNCTION__, NSStringFromCGRect(self.frame));
    
    // layoutSubviews中, 负责给视图布局
    // 不要使用cell的frame去进行视图的布局
    // 使用contentView的frame进行布局
//    CGFloat leftMargin = 5;
//    CGFloat rightMargin = 5;
//    CGFloat topMargin = 5;
//    CGFloat bottomMargin = 5;
//    CGFloat middleMargin = 5;
//    CGFloat titleLabelHeight = 30;
    _customView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, 200);
    _newsImageView.frame = CGRectMake(5, 5, (kWidth - 15) / 3, kHeight - 10);
    _titleLabel.frame = CGRectMake((kWidth - 15) / 3 + 10, 5, (kWidth - 15) * 2 / 3, (kHeight - 15) / 3);
    _descriptionLabel.frame = CGRectMake((kWidth - 15) / 3 + 10, (kHeight - 15) / 3 + 10, (kWidth - 15) * 2 / 3, (kHeight - 15) * 2 / 3);
    _numberLabel.frame = CGRectMake(kWidth - 65, kHeight - 65, 60, 60);
    
    // warning label上不要加视图
}
- (void)setNewsObject:(NewsObject *)newsObject {
    _newsObject = newsObject;
    _newsImageView.image = [UIImage imageNamed:newsObject.newsImage];
    _titleLabel.text = newsObject.newsTitle;
    _descriptionLabel.text = newsObject.newsDescription;
    _numberLabel.text = newsObject.newsNumberCount;
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
